$(document).ready(function() {
    var title;
    var content;
    var img;
    var url;
    var data;
    var style;
    var trsl;
    var alterid;
    // 获取新闻
    function getNews() {
        $.ajax({
            url: '/looknews',
            type: 'get',
            success: function(rows) {
                for (let i = 0; i < rows.length; i++) {
                    $('<tr><td>' + rows[i].id + '</td><td>' + rows[i].title + '</td><td>' + rows[i].time + '</td><td><button type="submit" class="btn btn-danger btn" data-toggle="modal" data-target="#myModal">删除</button>  <button type="submit" class="btn btn-success btn alter" >修改</button></td></tr>').addClass('antr').appendTo('tbody')
                }

            }
        })
    }
    getNews();
    //添加新闻
    function addnews() {
        $("body").on('click', '#submitbtn', function(event) {

            // $('#submitbtn').click(function() {
            title = $('#newstitle').val();
            content = $('#newscontent').val();
            img = $('#newsimg').val();
            url = $('#newsurl').val();
            data = $('#newsdata').val();
            style = $('#newsstyle').val();

            $.ajax({
                url: 'admin/addnews',
                type: 'post',
                data: {
                    newstitle: title,
                    newscontent: content,
                    newsimg: img,
                    newsurl: url,
                    newsdata: data,
                    newsstyle: style
                },
                success: function(data) {

                    if (data[0].id != '') {
                        $("#allnesid input").val('')
                        $.each(data, function(n, values) {
                            // console.log(values.id);
                            $('<tr><td>' + values.id + '</td><td>' + values.title + '</td><td>' + values.time + '</td><td><button type="submit" class="btn btn-danger btn" data-toggle="modal" data-target="#myModal">删除</button>  <button type="submit" class="btn btn-success btn alter" >修改</button></td></tr>').addClass('antr').appendTo('tbody')
                        })
                    }
                }
            })
        })
    }
    addnews();
    //删除新闻
    function deleteNews(id, rem) {
        $("body").on('click', 'tr td button', function(event) {
            id = $(this).parents('tr').children('td').eq(0).text();
            $('#btn-q').click(function() {
                func1(id)
            });
        });

    }
    //删除自动刷新
    function func1(id, rem) {
        var tr = $("tr").length;
        for (let i = 1; i < tr; i++) {
            var trrom = $("tr").eq(i).children('td').first()
            if (trrom.text() == id) {
                trrom.parents('tr').remove();
            }
        }
        $.ajax({
            url: '/admin/deleteNews',
            type: 'post',
            data: {
                myid: id
            },
            success: function(data) {
                console.log(data)
            }
        })
    }

    deleteNews();
    // 修改
    function alterNews() {
        $("body").on('click', '.alter', function() {
            $('.sss').removeAttr('id').addClass('butalter')
                // numb = $(this).parents('tr')
            trsl = $(this).parents('tr');
            alterid = $(this).parents('tr').children('td').eq(0).text();
            $.ajax({
                url: '/looknews',
                type: 'get',
                success: function(rows) {
                    for (let i = 0; i < rows.length; i++) {
                        if (rows[i].id == alterid) {
                            title = $('#newstitle').val(rows[i].title);
                            content = $('#newscontent').val(rows[i].content);
                            img = $('#newsimg').val(rows[i].img);
                            url = $('#newsurl').val(rows[i].url);
                            data = $('#newsdata').val(rows[i].time);
                            style = $('#newsstyle').val(rows[i].src);
                            //  nmb = row[i].id;

                        }
                    }
                }
            })
        });
        // function fun2(rows) {}
        $("body").on('click', '.butalter', function() {
            $(this).attr('id', 'submitbtn').removeClass('butalter');
            // alert(12)
            // console.log(trsl title = $('#newstitle').val();
            // trsl.children('td').val('')
            title = $('#newstitle').val();
            content = $('#newscontent').val();
            img = $('#newsimg').val();
            url = $('#newsurl').val();
            data = $('#newsdata').val();
            style = $('#newsstyle').val();
            // alert(img)
            $.ajax({
                url: 'admin/alternews',
                type: 'post',
                data: {
                    newstitle: title,
                    newscontent: content,
                    newsimg: img,
                    newsurl: url,
                    newsdata: data,
                    newsstyle: style,
                    alterid: alterid
                },
                success: function(data, numb) {
                    if (numb == 'success') {
                        // getNews()
                        $.each(data, function(index, el) {
                            trsl.children('td').eq(1).text(el.title);
                            trsl.children('td').eq(2).text(el.time);

                        });
                        // console.log(trsl)


                    }

                }
            });
        });
    }
    alterNews();
});